package com.eqdd.yiqidian.ui.tongxunlu;

import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.TeamNameSetActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.BaseBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.ToastUtil;

import android.net.Uri;
import android.view.View;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Group;

public class TeamNameSetActivity extends BaseActivity {
    private TeamNameSetActivityCustom dataBinding;
    private String groupid;
    private String groupname;

    @Override
    public void initBinding() {
        groupid = getIntent().getStringExtra("groupid");
        groupname = getIntent().getStringExtra("groupname");

        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_team_name_set);
        initTopTitleBar(View.VISIBLE, "修改群名称");
        initTopRightText("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("保存");
                new HttpPresneter.Builder<String>(HttpConfig.UPDATE_GROUP, String.class, TeamNameSetActivity.this)
                        .Params("groupid", groupid)
                        .Params("groupname", dataBinding.etName.getText().toString().trim())
                        .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                            @Override
                            public void onError(Throwable e) {
                                ToastUtil.showShort("服务器异常" + e.toString());
                            }

                            @Override
                            public void onNext(Object o) {
                                BaseBean baseBean = GsonUtils.changeGsonToBean((String) o, BaseBean.class);
                                ToastUtil.showShort(baseBean.getMsg());
                                if (baseBean.getStatus() == 200) {
                                    Intent intent = new Intent();
                                    intent.putExtra("groupname", dataBinding.etName.getText().toString().trim());
                                    setResult(Config.CHANGE, intent);
                                    Group group = new Group(groupid,
                                            dataBinding.etName.getText().toString().trim(), Uri.parse("0"));
                                    RongIM.getInstance().refreshGroupInfoCache(group);
                                    finish();
                                }
                                ToastUtil.showShort("修改信息成功");
                            }
                        })
                        .build()
                        .post();


            }
        });
        dataBinding.etName.setText(groupname);
    }

    @Override
    public void initData() {
    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
